package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 *
 */
public class HKgoldenAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.hkgolden.com/view.aspx\\?message=[0-9]*.*",
		"http://.*.hkgolden.com/view.aspx\\?message=[0-9]*",
		"http://.*.hkgolden.com/articles/article.aspx\\?catid=[0-9]*&id=[0-9]*",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("title").text();
		if(title!=null) title=title.replace(" - 香港高登討論區", "");
//		if (title.contains("-")){
//			title=StringUtils.substringBefore(title, "-");
//		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.ContentGrid"));
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select(".href_txt_blog2"));


		if (date==null)date =JSoupUtils.matchDate(doc, "標籤：");
		if (date==null)date =DateUtils.matchDate(doc.select("span[style=font-size: 12px; color:gray;]").text());
		if (date==null)date =DateUtils.matchDate(doc.select("div[style=clear:both; color: Gray; font-size: 16px;]").text());
		try {
			author=doc.select(".repliers_left a[href^=javascript: ToggleUserDetail(").first().text();
		} catch (Exception e) {
		}
		if(StringUtils.isBlank(author)) author=JSoupUtils.matchAuthor(doc, "文：");

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
